//
//  MLTabBarController.swift
//  MLSina20160623
//
//  Created by manmanlang on 16/6/23.
//  Copyright © 2016年 manmanlang. All rights reserved.
//

import UIKit

class MLTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        let tabbar = MLTabBar()
        
        //赋值tabbar
        setValue(tabbar, forKey: "tabBar")
        
        tabbar.composeButton.addTarget(self, action:#selector(MLTabBarController.composeButtonClick), forControlEvents: UIControlEvents.TouchDown)
        
        allChildViewController()
    }
    
    /*  如果你的方法或类是private修饰，那么在Swift(静态语言)中则是不可见方法，会报一个找不到该方法的错误，可以使用@objc告知运行时找到对应的方法。
        而如果你用 Swift 写的 class 是继承自 NSObject 的话，Swift 会默认自动为所有的非 private 的类和成员加上 @objc 对于一个继承自object的类，你只需要导入对应的头文件就可以使用这个类了。
     @objc 修饰符的另一个作用是为 Objective-C 侧重新声明方法或者变量的名字。
     Objective-C 的话是无法使用中文来进行调用的，因此我们必须使用 @objc 将其转为 ASCII 才能在 Objective-C 里访问
    */
    @objc private func composeButtonClick() {
        let navigation = UINavigationController(rootViewController: MLComposeController())
        presentViewController(navigation, animated: true, completion: nil)
    }
    
    func allChildViewController() {
        
        addChildViewController(MLHomeController(), name: "首页", image: "tabbar_home")
        addChildViewController(MLMessageController(), name: "消息", image: "tabbar_message_center")
        addChildViewController(MLDiscoverController(), name: "发现", image: "tabbar_discover")
        addChildViewController(MLSettingController(), name: "设置", image: "tabbar_profile")
        
    }
    
    func addChildViewController(childController: UIViewController,name:String,image:String) {
        
        childController.title = name
        childController.tabBarItem.image = UIImage(named: image)
        
        let selImage = UIImage(named: "\(image)_highlighted")
        childController.tabBarItem.selectedImage = selImage?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        let nav = UINavigationController(rootViewController:childController)
        addChildViewController(nav)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
